import { userAccountLogin } from '@/api/user'
// 用户状态
const user = {
  namespaced: true,
  // 存数据
  state: () => ({
    profile: {
      id: '',
      nickname: '',
      avatar: '',
      token: '',
      mobile: ''
    }
  }),
  // 唯一能改变state的数据
  mutations: {
    setUser (state, payload) {
      // state.id = 10001
      state.profile = payload
    },
    // 清空用户数据
    clearUserInfo (state) {
      // 1.vuex 2.localStorage
      state.profile = {}
    }
  },
  // 异步 + mutation
  actions: {
    async asyncSetUser (ctx, payload) {
      // 依赖于参数
      // 解构参数
      const res = await userAccountLogin(payload)
      // return new Promise()
      ctx.commit('setUser', res.result)
    }
  },
  // 基于state计算属性
  getters: {}
}

export default user
